import React, {useContext, useRef, useEffect} from 'react';
import {
  View,
  PanResponder,
  Alert,
  Platform,
  ScrollView,
  FlatList,
} from 'react-native';
import dayjs from 'dayjs';
import Toast from 'react-native-root-toast';
import {useNavigation} from '@react-navigation/native';
import {useDebounceEffect, useMount, useUnmount, useSafeState} from 'ahooks';
import {Text, Button, Input, Dialog, ListItem, Avatar} from '@rneui/themed';
import FontAwesome5 from 'react-native-vector-icons/FontAwesome5';
import {useSelector, useDispatch} from 'react-redux';
import AsyncStorage from '@react-native-async-storage/async-storage';
import GlobalContext from '@/GlobalContext';
import ImagePicker from 'react-native-image-crop-picker';
import fileExtension from 'file-extension';
import {ApiHost, OssHost} from '@/config/common';
import {setKeyGetAccountInfo} from '@/store';
import DocumentPicker from 'react-native-document-picker';

import RNFS from 'react-native-fs';
import RNFetchBlob from 'react-native-blob-util';
import {round} from 'lodash';
import {Dirs, FileSystem} from 'react-native-file-access';

import getDirsInfoSystem from './getDirsInfoSystem';
export default () => {
  const {localMongoDbFileSystem} = useContext(GlobalContext);

  const [loading, setLoading] = useSafeState(false);
  // end: 2023-09-18T15:21:14+08:00 use 199512 ms total 57524
  // end: 2023-09-18T15:26:02+08:00 use 181646 ms total 52836

  return (
    <>
      <Button
        loading={loading}
        title="UpdateSDcardDir"
        onPress={async () => {
          setLoading(true);
          const db = localMongoDbFileSystem;

          const sdCardPath = Dirs.SDCardDir;
          // const oneDirInfo = await RNFS.stat(sdCardPath);
          // sdcard/电影
          const oneDirInfo = {
            path: sdCardPath,
          };
          const oneDirs = await getDirsInfoSystem({pPath: sdCardPath});
          const oneLevelLastItem = oneDirs[oneDirs.length - 1];
          const fun = async ({fileInfo = {}, list = [], isLastDir = false}) => {
            const lastItem = list[list.length - 1];
            for (const nowItem of new Set(list)) {
              // if (nowItem.path !== lastItem.path) {
              //   // console.log('不是最后', nowItem.path);
              // } else {
              //   console.log('这是最后', nowItem.path);
              // }

              let flag = (nowItem.path + '').startsWith(oneLevelLastItem.path);
              // flag &&= nowItem.path === lastItem.path;
              // console.log(nowItem.path);
              if (flag) {
                console.log('这是最后', nowItem.path);
              }

              const sonDirs = await getDirsInfoSystem({pPath: nowItem.path});
              if (sonDirs.length > 0) {
                fun({list: sonDirs});
              }
            }
          };
          fun({fileInfo: oneDirInfo, list: oneDirs});
          // setTimeout(() => {
          //   console.log('一级最后', oneLevelLastItem.path);
          // }, 3000);

          // console.log(oneDirs.length);
          // let total = 0;
          // const depthSetFiles = async (pPath, array) => {
          //   const systemFiles = await getDirsInfoSystem({pPath});
          //   systemFiles.forEach(item => {
          //     total++;
          //     console.log(total, item.path);
          //   });

          //   for (const item of new Set(systemFiles)) {
          //     depthSetFiles(item.path);
          //   }
          // };

          // depthSetFiles(Dirs.SDCardDir);

          setLoading(false);
        }}
        containerStyle={{
          margin: 20,
        }}
      />
    </>
  );
};
